var PaperModule = function () {}();


//试题选择单击事件
function inputClick(){
	var flag = true;
	$("#modelEditable").find("tr").each(function () {
		var arrtd = $(this).children();
		var con = arrtd.eq(0).text();
		if(con !="全选"){
			var v = arrtd.eq(0).find("input");
			var curFlag = v[0].checked;
			if(curFlag == false){
				flag = false;
			}
		}
	});
	if(flag){
		$("#btn_all_check")[0].checked = true;
	}else{
		$("#btn_all_check")[0].checked = false;
	}
}

$("#btn_all_check").click(function(){
	var flag = $("#btn_all_check")[0].checked;
	$("#modelEditable").find("tr").each(function () {
		var arrtd = $(this).children();
		var con = arrtd.eq(0).text();
		if(con !="全选"){
			var v = arrtd.eq(0).find("input");
			if(flag == true){
				v[0].checked = true;
			}else{
				v[0].checked = false;
			}
		}
	});
});

//新增问卷
function BtnAddQuestion(){
	var questions = new Array();
	$("#column1 li").each(function(){
		var id = $(this).attr("id");
		if(id != undefined){
			questions.push(id);
		}
	})
	if(questions.length <= 0){
		showError("请选择需要添加到试卷的题目！",2000,true);
		return;
	}
	var paperName = $("#paperName").val();
	if(paperName == ""){
		showError("风险测评问卷名称不能为空！",2000,true);
		return;
	}
	bootbox.confirm("你确定要添加所选择的试题到测评试卷: "+paperName+" 吗?", function(result) {
		if(result){
			var json = "{\"paperName\":\""+paperName+"\",\"questions\":\""+questions+"\"}";
            $.ajax({
            	type:"post",
                contentType:"application/json; charset=UTF-8",
                url: ctx + "/question/addPaper",
                data:json,
                dataType:"json",
                success: function(msg){
               	 if(msg.code == 0){
               		 showError(msg.message,2000,true);
               	 }else{
               		showTips("新增测评试卷成功.",2000,true);
               		setTimeout("location.reload()",2000);
               	 }
                },
                error: function (msg) {
                	showError("服务器出错了,删除试题失败了!",2000,true);
                }
            });
       	}
        //$(this).closeDialog(modal); 
    });
}



//页面初始化时加载试题
$(function(){
	$.ajax({
    	contentType:"application/json; charset=UTF-8",
        dataType : 'json',
        type : "POST",
        url : ctx + "/question/mylist",
        success: function(msg){
        	var aData = msg.aaData;
        	var str ="<li class=\"handle disabled\" ><div class=\"dd-handle\"><span class=\"pull-right\">直接不合格选项</span>" +
			"<span class=\"label label-info\">未选题目</span></div></li>";
        	var str = "";
        	for(var i=0;i<aData.length;i++){
        		var opt = "";
        		var quart = "";
        		if(aData[i].options){
        			opt = "多选";
        		}else{
        			opt = "单选";
        		}
        		var unqualified = aData[i].unqualified
        		if(unqualified == ""){
        			quart = "无";
        		}else{
        			quart = unqualified;
        		}
        		str = str +"<li class=\"handle\" id=\""+aData[i].id+"\"><div class=\"dd-handle\"  onclick=\"questionOnclick('"+aData[i].id+"');\"><span class=\"pull-left\">("+opt+")</span><span class=\"pull-right\">"+quart+"</span>"+
        				"<span class=\"label label-info\"></span>"+aData[i].title+"</div></li>";
        	}
        	var s = str;
        	document.getElementById('column3').innerHTML = str;
        	// 初始化Sortables
        	$(".column").sortable({
        		connectWith:	$(".column"),	
        		placeholder:	'widget-placeholder',	
        		cursor:			'move',	
        		dropOnEmpty :   true,
        		helper:			function (evt, ui) { return $(ui).clone().appendTo('body').show(); },	
        		over:			function (evt, ui) {
        							var
        								$target_UL	= $(ui.placeholder).parent(),	
        								targetWidth	= $target_UL.width(),	
        								helperWidth	= ui.helper.width(),	
        								padding		= parseInt( ui.helper.css('paddingLeft') )
        											+ parseInt( ui.helper.css('paddingRight') )
        											+ parseInt( ui.helper.css('borderLeftWidth') )
        											+ parseInt( ui.helper.css('borderRightWidth') )
        							;
        							//if (( (helperWidth + padding) - targetWidth ) > 20)
        							ui.helper.height('auto').width( targetWidth - padding );
        						}
        	});

        	$("#draggable").draggable({
        		helper:	function () { return $(this).clone().appendTo('body').css('zIndex',5).show(); },	
        		cursor:	'move',	
        		dropOnEmpty :   true
        	});

        	$('#droppable').droppable({ 
        	   accept:	'#draggable',  
        	   dropOnEmpty :   true,  
        	   drop:	function () { alert('The Draggable was Dropped!'); } 
        	}); 
        },
        error: function (msg) {
        	showError("服务器出错了,获取试题失败了!",2000,true);
        }
    });
	

})

function questionOnclick(id){
	var table = document.getElementById("detail_options_table");
    var tableLength = table.rows.length;
    for(var int = 1;int < tableLength;int++) {
       table.deleteRow(1);
    }	
	var json = "{\"id\":\""+id+"\"}";
    $.ajax({
    	type:"post",
        contentType:"application/json; charset=UTF-8",
        url: ctx + "/question/detail",
        data:json,
        dataType:"json",
        success: function(msg){
       	$("#btn_detail_Modal").click();
        	var data = msg.data;
        	var code = msg.code;
        	if(code == 1){
        		var title = data.question.title;
        		var opts = data.question.options;
        		var options = data.optList;
        		$("#detail_title").html(title);
        		if(!opts){
        			$("#detail_isMultChoice").html("否");
        		}else{
        			$("#detail_isMultChoice").html("是");
        		}
        		for(var i=options.length-1;i>=0;i--){
        			var isMult = "否";
        			if(options[i].unqualified == 1){
        				isMult = "是";
        			}
        			var tableStr= "<tr role=\"row\" class=\"odd\"><td style=\"font-size:13px;\"> <center>"+options[i].no+"</center></td>"+
        			"<td style=\"font-size:13px;\">"+options[i].content+"</td>"+
        			"<td style=\"font-size:13px;\"><center>"+options[i].score+"</center></td>"+
        			"<td style=\"font-size:13px;\"><center>"+isMult+"</center></td>"+
        			"</tr>";
        			$("#detail_options_table tr:eq(0)").after(tableStr);
        		}
        	}else{
        		showError("服务器出错了,查看试题详情失败了!",2000,true);
        	}
        },
        error: function (msg) {
        	showError("服务器出错了,查看试题详情失败了!",2000,true);
        }
    });
}
